﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WebEditBackGround
{
    public partial class Frm_产品类别管理 : Form
    {
        public Frm_产品类别管理()
        {
            InitializeComponent();
        }

        private void Frm_产品类别管理_Load(object sender, EventArgs e)
        {
            try
            {
                SC.FF_DGV_格式设置(DGV_产品类别, false);
                FF_加载已有产品类别();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        SysClass SC = new SysClass();

        DataTable dt_当前已有产品类别 = new DataTable();

        private void help()
        {
            try
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void FF_加载已有产品类别()
        {
            try
            {
                // /api/goodstype.select.all

                string cs_postUrl = SysClass.cx_url_post + "/api/goodstype.select.all";
                Code.Model.Base.RequestBase _request = new Code.Model.Base.RequestBase();

                string cs_in = JsonConvert.SerializeObject(_request);
                string cs_out = SC.FF_PostWebRequest(cs_postUrl, cs_in);

                Code.Model.ProductClass.Response_ProductClassList _response = JsonConvert.DeserializeObject<Code.Model.ProductClass.Response_ProductClassList>(cs_out);

                if (_response.TB_GoodsTypeList != null)
                {
                    dt_当前已有产品类别 = SC.FF_JsonToDataTable<Code.Model.ProductClass.TableModel_ProductClassList>(_response.TB_GoodsTypeList);
                    DGV_产品类别.DataSource = dt_当前已有产品类别;
                }
                else
                {
                    DataTable dtclear = dt_当前已有产品类别.Clone();
                    DGV_产品类别.DataSource = dtclear;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void but_筛选_Click(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void but_新增_Click(object sender, EventArgs e)
        {
            try
            {
                // goodstype.add
                string cs_postUrl = SysClass.cx_url_post + "/api/goodstype.add";


                Code.Model.ProductClass.Request_ProductClass_Add _request = new Code.Model.ProductClass.Request_ProductClass_Add();
                _request.GoodsTypeName = tb_类别名称.Text;

                string cs_in = JsonConvert.SerializeObject(_request);
                string cs_out = SC.FF_PostWebRequest(cs_postUrl, cs_in);

                Code.Model.Base.BaseResponse _response = JsonConvert.DeserializeObject<Code.Model.Base.BaseResponse>(cs_out);

                if (_response.ReturnValue == true)
                {
                    FF_加载已有产品类别();
                }
                else
                {
                    MessageBox.Show(_response.ErrorInfo.ErrorMsg);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void but_编辑_Click(object sender, EventArgs e)
        {
            try
            {
                string csStr01 = DGV_产品类别.SelectedRows[0].Cells["GoodsTypeName"].Value.ToString();
                string csStr02 = tb_类别名称.Text;

                string csStrSure = "您确认要把【" + csStr01 + "】改成【" + csStr02 + "】吗";

                if (MessageBox.Show(csStrSure, "操作确认", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }

                string csStrID = DGV_产品类别.SelectedRows[0].Cells["GoodsTypeID"].Value.ToString();

                Code.Model.ProductClass.Request_ProductClass_Edit _request = new Code.Model.ProductClass.Request_ProductClass_Edit();
                _request.GoodsTypeID = csStrID;
                _request.GoodsTypeName = csStr02;
                _request.GoodsTypeName_Old = csStr01;

                // goodstype.type.edit
                string cs_postUrl = SysClass.cx_url_post + "/api/goodstype.type.edit";

                string cs_in = JsonConvert.SerializeObject(_request);
                string cs_out = SC.FF_PostWebRequest(cs_postUrl, cs_in);

                Code.Model.Base.BaseResponse _response = JsonConvert.DeserializeObject<Code.Model.Base.BaseResponse>(cs_out);

                if (_response.ReturnValue == true)
                {
                    FF_加载已有产品类别();
                }
                else
                {
                    MessageBox.Show(_response.ErrorInfo.ErrorMsg);
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void but_删除_Click(object sender, EventArgs e)
        {
            try
            {
                


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void but_停用_Click(object sender, EventArgs e)
        {
            try
            {
                string csStr01 = DGV_产品类别.SelectedRows[0].Cells["GoodsTypeName"].Value.ToString();
                string csStrID = DGV_产品类别.SelectedRows[0].Cells["GoodsTypeID"].Value.ToString();

                if (MessageBox.Show("您确认要停用【" + csStr01 + "】类别吗？", "停用确认", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }

                // /api/goodstype.type.del

                Code.Model.ProductClass.Request_ProductClass_Delete _request = new Code.Model.ProductClass.Request_ProductClass_Delete();
                _request.GoodsTypeID = csStrID;

                string cs_postUrl = SysClass.cx_url_post + "/api/goodstype.type.del";

                string cs_in = JsonConvert.SerializeObject(_request);
                string cs_out = SC.FF_PostWebRequest(cs_postUrl, cs_in);

                Code.Model.Base.BaseResponse _response = JsonConvert.DeserializeObject<Code.Model.Base.BaseResponse>(cs_out);

                if (_response.ReturnValue == true)
                {
                    FF_加载已有产品类别();
                }
                else
                {
                    MessageBox.Show(_response.ErrorInfo.ErrorMsg);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DGV_产品类别_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (DGV_产品类别.SelectedRows.Count == 0)
                {
                    return;
                }
                tb_类别名称.Text = DGV_产品类别.SelectedRows[0].Cells["GoodsTypeName"].Value.ToString();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
